package connectx

import (
	"google.golang.org/protobuf/compiler/protogen"
	"strings"
)

var generatedMethods = make(map[string]bool)

func MarkMethodGenerated(methodName string) {
	generatedMethods[methodName] = true
}
func IsMethodGenerated(methodName string) bool {
	_, ok := generatedMethods[methodName]
	return ok
}

func GetHttpResponseWriter() protogen.GoIdent {
	return HttpPackage.Ident("ResponseWriter")
}

func GetHttpRequest() protogen.GoIdent {
	return HttpPackage.Ident("Request")
}

func GetHttpHandlerFunc() protogen.GoIdent {
	return HttpPackage.Ident("HandlerFunc")
}

func GetHttpHandler() protogen.GoIdent {
	return HttpPackage.Ident("Handler")
}

func GetStringToLower() protogen.GoIdent {
	return StringPackage.Ident("ToLower")
}

func GetStringTrim() protogen.GoIdent {
	return StringPackage.Ident("TrimRight")
}

func GetCtx() protogen.GoIdent {
	return ContextPackage.Ident("Context")
}

func GetServiceDescriptor() protogen.GoIdent {
	return ProtoreflectPackage.Ident("ServiceDescriptor")
}
func GetMethodDescriptor() protogen.GoIdent {
	return ProtoreflectPackage.Ident("MethodDescriptor")
}

func GetConnectIdempotent() protogen.GoIdent {
	return ConnectPackage.Ident("IdempotencyIdempotent")
}

func GetConnectNewUnaryHandler() protogen.GoIdent {
	return ConnectPackage.Ident("NewUnaryHandler")
}

func GetConnectHandlerOption() protogen.GoIdent {
	return ConnectPackage.Ident("HandlerOption")
}

func GetConnectEffects() protogen.GoIdent {
	return ConnectPackage.Ident("IdempotencyNoSideEffects")
}

func GetConnectIdempotency() protogen.GoIdent {
	return ConnectPackage.Ident("WithIdempotency")
}

func GetConnectSchema() protogen.GoIdent {
	return ConnectPackage.Ident("WithSchema")
}

func GetConnectNewClient() protogen.GoIdent {
	return ConnectPackage.Ident("NewClient")
}

func GetConnectWithHandlerOptions() protogen.GoIdent {
	return ConnectPackage.Ident("WithHandlerOptions")
}

func GetConnectWithClientOption() protogen.GoIdent {
	return ConnectPackage.Ident("WithClientOptions")
}

func GetConnectClientOption() protogen.GoIdent {
	return ConnectPackage.Ident("ClientOption")
}

func GetConnectClient() protogen.GoIdent {
	return ConnectPackage.Ident("HTTPClient")
}

func GetReq() protogen.GoIdent {
	return ConnectPackage.Ident("Request")
}

func GetRsp() protogen.GoIdent {
	return ConnectPackage.Ident("Response")
}

func GetIoEOF() protogen.GoIdent {
	return IoPackage.Ident("EOF")
}

func GetIoNopCloser() protogen.GoIdent {
	return IoPackage.Ident("NopCloser")
}

func Unexport(s string) string {
	lowercased := strings.ToLower(s[:1]) + s[1:]
	switch lowercased {
	// https://go.dev/ref/spec#Keywords
	case "break", "default", "func", "interface", "select",
		"case", "defer", "go", "map", "struct",
		"chan", "else", "goto", "package", "switch",
		"const", "fallthrough", "if", "range", "type",
		"continue", "for", "import", "return", "var":
		return "_" + lowercased
	default:
		return lowercased
	}
}
